package cn.edu.scau.sec.rw.view;

import cn.edu.scau.sec.rw.model.disk_file.disk.Disk;
import cn.edu.scau.sec.rw.model.disk_file.file.FileCatalog;
import cn.edu.scau.sec.rw.model.disk_file.file.VirtualFile;
import cn.edu.scau.sec.rw.model.disk_file.utils.File_DiskUtil;
import javafx.animation.FadeTransition;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Paint;
import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

public class CreateCatalogUI {

    double x=0 ,y=0;
    final private TextField filename = new TextField();
    private TreeItem<VirtualFile> item;
    private Stage stage;
    private Label alarm;
    private Button setTrue, setFalse;
    private GridPane gridPane;

    public CreateCatalogUI(TreeItem<VirtualFile> item){
        this.item = item;
        stage = new Stage();
        gridPane = new GridPane();
        alarm = new Label();
        alarm.setStyle("-fx-text-fill:#FF0000");
        setTrue = new Button("完成");
        setFalse = new Button("取消");
        setStage();
    }

    private void setStage() {
//        gridPane.setId("gpane");
//        setTrue.setId("Submit");
//        setFalse.setId("Cancel");
        gridPane.setPadding(new Insets(10, 10, 10, 10));
        gridPane.setVgap(4);
        gridPane.setHgap(4);
        Label label1 = new Label("名称");
        GridPane.setConstraints(label1, 0, 0);
        label1.setStyle("-fx-text-fill:#ffffff");
        filename.setPromptText("文件名");
        filename.setPrefColumnCount(20);
        filename.setTooltip(new Tooltip("请输入文件名。"));
        GridPane.setConstraints(filename, 1, 0);
        GridPane.setConstraints(filename,1,1);
        GridPane.setConstraints(setTrue, 0, 2);
        GridPane.setConstraints(setFalse, 1, 2);
        gridPane.getChildren().addAll(label1,filename,setTrue,alarm,setFalse);

        initSetTrue(setTrue);

        initSetFalse(setFalse);

        Scene scene=new Scene(gridPane);
        scene.getStylesheets().add("cn/edu/scau/sec/rw/view/css/css.css");
        scene.setFill(Paint.valueOf("#00000000"));
//        URL url2 =getClass().getClassLoader().getResource("icon/鼠标.png");
//        String path2 = url2.toExternalForm();
//        scene.setCursor(Cursor.cursor(path2));
        gridPane.setOnMousePressed(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                // TODO 自动生成的方法存根
                if(event.getButton().name().equals(MouseButton.PRIMARY.name())){
                    x = event.getScreenX()-stage.getX();
                    y = event.getScreenY()-stage.getY();
                }
            }
        });

        gridPane.setOnMouseDragged(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                // TODO 自动生成的方法存根
                if(event.getButton().name().equals(MouseButton.PRIMARY.name())){
                    if(event.getScreenX()<= Screen.getPrimary().getVisualBounds().getMaxX()) stage.setX(event.getScreenX()-x);
                    if(event.getScreenY()<=Screen.getPrimary().getVisualBounds().getMaxY()) stage.setY(event.getScreenY()-y);
                }
            }
        });
        stage.setTitle("重命名");
        stage.setScene(scene);
        stage.setAlwaysOnTop(true);
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.initModality(Modality.APPLICATION_MODAL);
//        stage.getIcons().add(new Image("icon/编辑图标.png"));
        stage.setTitle("RenameInterface");
        stage.show();
        FadeTransition fade = new FadeTransition();
        fade.setDuration(Duration.seconds(0.2));
        fade.setFromValue(0);
        fade.setToValue(1);
        fade.setNode(gridPane);
        fade.play();
    }

    private void initSetFalse(Button setFalse) {
        setFalse.setOnAction(event -> {
            stage.close();
        });
    }

    private void initSetTrue(Button setTrue) {

        setTrue.setOnAction(event -> {

            VirtualFile fileCatalog = item.getValue();
            FileCatalog catalog = new FileCatalog(filename.getText(), "目录项内容");
            ((FileCatalog) fileCatalog).add(catalog);
            File_DiskUtil.saveVirtualFile(catalog);
            for (int i : Disk.getDisk().getFAT()) {
                System.out.print(i+" ");
            }
            System.out.println();
            TreeItem<VirtualFile> treeItem = new TreeItem<>(catalog);
            item.getChildren().add(treeItem);
            stage.close();
        });
    }
}
